<?php
namespace Client\Controller;

use Client\Common\VerifyHelper;
use Zend\Mvc\MvcEvent;

class InitializeController extends CommonController
{
    protected $user_id;
    
    protected function attachDefaultListeners(){
        parent::attachDefaultListeners();
        $events = $this->getEventManager();
        $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 100);
    }
    
    public function preDispatch(){
        $route = $this->event->getRouteMatch()->getMatchedRouteName();
    
        //登入页面不验证
        if($route == 'wechat') {
            return;
        }
        
        $verify_is_login = new VerifyHelper();
        $this->user_id = $verify_is_login->isLogin();

        if(empty($this->user_id)){
            //获取当前url地址，保存到session，登入后，跳回当前url
            $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $_SESSION['redirect_uri'] = $redirect_uri;
            return $this->redirect()->toRoute('wechat', array(
                'action' => 'wechatLogin',
            ));
        }
    
    }

}